package com.it.myd.controller.main;

import com.it.myd.bean.main.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.servlet.http.Part;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/gameinfo")
public class GameInfoController {

    @RequestMapping("/uploadImage")
    @ResponseBody
    public Map<String, Object> uploadImage(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws Exception {
        Part part = request.getPart("file");
        String filename=part.getSubmittedFileName();
//        String filePath="E:/mydream/img/articleimg/";
        String filePath="/usr/myd/img/gameinfo/";
        File uploadedFile= new File(filePath);
        if(!uploadedFile.exists()){
            uploadedFile.mkdir();
        }
        //上传文件到指定目录
        part.write(filePath+"/"+filename);
        Map<String, Object> ret = new HashMap<>();
        ret.put("location", "/gameinfo/"+filename);
        return ret;
    }

}
